package com.woniu.woniu_bx.controller;

import com.woniu.woniu_bx.pojo.Goods;
import com.woniu.woniu_bx.pojo.HelperQues;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.pojo.ResponseEntity;
import com.woniu.woniu_bx.service.HelperQuesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Description 帮助中心类别
 * @Author zhangjin
 * @Date 2022/8/29 14:13
 */
@RestController
//@EnableGlobalMethodSecurity(prePostEnabled = true)
public class HelpQueController {
    @Autowired
    private HelperQuesService helperQuesService;

    /**
     * 查询单个问题
     * @param id
     * @return
     */
    @GetMapping("/getQues/{id}")
    public ResponseEntity getQuesByID(@PathVariable Integer id){
        if (id!=null){
            HelperQues result = helperQuesService.getByID(id);
            return ResponseEntity.SUCCESS("操作成功",ResponseEntity.SUCCESS("操作成功",result));
        }
        return ResponseEntity.FAIL("id无效",null);
    }

    /**
     * 查询相同类别的问题
     * @param categoryId
     * @return
     */
    @GetMapping("/getQuesList/{categoryId}")
//    @PreAuthorize("hasAnyAuthority('helper:quesList')")
    public ResponseEntity getQuesByCID(@PathVariable Integer categoryId){
        if (categoryId!=null){
            List<HelperQues> result = helperQuesService.getByCategoryId(categoryId);
            return ResponseEntity.SUCCESS("操作成功",result);
        }
        return ResponseEntity.FAIL("类别id无效",null);
    }

    /**
     * 增加问答
     * @param helperQues
     * @return
     */
    @PostMapping("/admin/addHelperQues")
    @PreAuthorize("hasAnyAuthority('helper:quesEdit')")
    public ResponseEntity addQues(@RequestBody @Valid HelperQues helperQues){
        helperQuesService.add(helperQues);
        return ResponseEntity.SUCCESS("添加成功",null);
    }

    /**
     * 更新问答
     * @param helperQues
     * @return
     */
    @PostMapping("/admin/updateHelperQues")
    @PreAuthorize("hasAnyAuthority('helper:quesUpdate')")
    public ResponseEntity updateQues(@RequestBody HelperQues helperQues){
        helperQuesService.update(helperQues);
        return ResponseEntity.SUCCESS("修改成功",null);
    }

    /**
     * 返回所有的类别
     * @return
     */
    @PostMapping("/admin/queslist")
    public ResponseEntity getCateList(@RequestBody PageBean pageBean){
        return ResponseEntity.SUCCESS("操作成功",helperQuesService.getQuesByPage(pageBean));
    }

    /**
     * 前台app帮助中心获取所有问题名
     * @return
     */
    @RequestMapping("/getAllQuesName")
    public ResponseEntity getAllQuesName(){
        return ResponseEntity.SUCCESS("ok",helperQuesService.getAllQuesName());
    }
    /**
     * 删除问答
     */
    @PostMapping("/admin/delQues")
    @PreAuthorize("hasAnyAuthority('helper:quesDel')")
    public ResponseEntity delQues(@RequestBody HelperQues helperQues){
        return ResponseEntity.SUCCESS("删除成功",helperQuesService.removeById(helperQues));
    }
    /**
     * 批量删除
     */
    @PostMapping("/admin/delAllSelect")
    @PreAuthorize("hasAnyAuthority('helper:quesDel')")
    public ResponseEntity delAllSelect(@RequestBody int[] ids){
        List<Integer> idList = new ArrayList<>();
        for (int id : ids) {
            idList.add(id);
        }
        return ResponseEntity.SUCCESS("删除成功",helperQuesService.removeBatchByIds(idList));
    }
}
